package com.mzbopen.rightbottommoreview;

import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;


public class RightAddMoreHorizontalScrollView extends FrameLayout {
    private SmartScrollView right_add_more_sv;
    private LinearLayout right_add_more_content;
    private LinearLayout right_add_more_lin;
    private TextView right_add_more_add_more;
    private LineToTriangleView right_add_more_add_line_to_triangle;
    private BottomListenerCallBack bottomListenerCallBack;

    public RightAddMoreHorizontalScrollView(@NonNull Context context) {
        super(context);
        loadView();

    }

    public RightAddMoreHorizontalScrollView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        loadView();

    }

    public RightAddMoreHorizontalScrollView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        loadView();

    }

    public RightAddMoreHorizontalScrollView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        loadView();

    }
    public void setBottomListenerCallBack(BottomListenerCallBack bottomListenerCallBack) {
        this.bottomListenerCallBack = bottomListenerCallBack;
    }
    int target_width = 0;
    int now_width = 0;

    private void loadView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.right_add_more_horizontal_scrolview, this);
        right_add_more_lin = view.findViewById(R.id.right_add_more_lin);
        right_add_more_content = view.findViewById(R.id.right_add_more_content);
        right_add_more_add_more = view.findViewById(R.id.right_add_more_add_more);
        right_add_more_add_line_to_triangle = view.findViewById(R.id.right_add_more_add_line_to_triangle);
        right_add_more_sv = view.findViewById(R.id.right_add_more_sv);
        right_add_more_sv.setTouchUpIsRightListener(() -> {
            if (bottomListenerCallBack!=null)bottomListenerCallBack.onBottomListenerCallBack();
//            Util.setWebIntent(getContext(), "https://www.baidu.com/");
            target_width = right_add_more_content.getMeasuredWidth() - (right_add_more_sv.getMeasuredWidth());
            now_width = right_add_more_lin.getMeasuredWidth() - (right_add_more_sv.getMeasuredWidth());
            new Handler().post(new Runnable() {
                @Override
                public void run() {
                    right_add_more_sv.scrollTo(now_width, 0);
                    now_width -= 2;
                    if (now_width <= target_width) {
                        now_width = target_width;
                        right_add_more_sv.scrollTo(now_width, 0);
                    }
                    if (now_width > target_width) new Handler().postDelayed(this, 5);
                }
            });
        });
        right_add_more_sv.setOverScrolledListener(new SmartScrollView.OverScrolledListener() {
            @Override
            public void onOverScrolledListener(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {

                right_add_more_add_line_to_triangle.setNewLocation(((float) right_add_more_sv.getWidth() - (right_add_more_lin.getWidth() - scrollX)) / right_add_more_add_line_to_triangle.getWidth());


            }
        });

    }


    public void addContent(List<View> models) {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        right_add_more_content.removeAllViews();
        if (right_add_more_content != null && models != null && models.size() != 0) {

            for (View view : models) {
                view.setLayoutParams(params);
                right_add_more_content.addView(view, params);

            }


        }

    }
}
